package cn.pn.project.controller;

import cn.pn.project.client.comment.CommentFeign;
import cn.pn.project.common.Dto;
import cn.pn.project.common.DtoUtil;
import cn.pn.project.common.Page;
import cn.pn.project.pojo.comment.Comment;
import cn.pn.project.pojo.comment.Like;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/comment")
public class CommentController {
    @Autowired
    private CommentFeign commentFeign;

    /**
     * 所有主评论
     * @param commodityId
     * @param pageNo
     * @param pageSize
     * @return
     */
    @RequestMapping(value = "/listMain",method = RequestMethod.POST)
    public String listMainComment(@RequestParam("commodityId") Integer commodityId,@RequestParam("pageNo") Integer pageNo,@RequestParam("pageSize") Integer pageSize){
        System.out.println("-----------consumer的/comment/listMain方法-----------");
        Page<Comment> page=commentFeign.listMainComment(commodityId,pageNo,pageSize);
        return JSONArray.toJSONString(page,true);
    }

    /**
     * 查询某一主评论的所有子评论
     * @param belongToId 所属主评论id
     * @param commodityId 商品id
     * @param pageNo 当前页数
     * @param pageSize 页面容量
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/listChild",method = RequestMethod.POST)
    public String listChildComment(@RequestParam("belongToId") Integer belongToId,@RequestParam("commodityId")Integer commodityId,@RequestParam("pageNo") Integer pageNo,@RequestParam("pageSize") Integer pageSize) throws Exception {
        System.out.println("-----------consumer的/comment/listChild方法-----------");
        return JSONArray.toJSONString(commentFeign.listChildComment(belongToId,commodityId,pageNo,pageSize),true);
    }


    /**
     * 查找某用户对某商品有无评论
     * @param userId 用户id
     * @param commodityId 商品id
     * @return 评论对象
     * @throws Exception
     */
    @RequestMapping(value = "/findMain",method = RequestMethod.POST)
    public String isCommentOn(@RequestParam("userId")Integer userId,@RequestParam("commodityId")Integer commodityId) throws Exception {
        //查找出来所有的主评论，在绑定数据时根据用户id匹配集合中的用户id
        //如果绑定后未找到该用户对这个商品的评论，显示我要评论，若点击则为添加
        //如果找到了则显示我的评论，若点击则获取该评论
        return JSON.toJSONString(commentFeign.isCommentedOn(userId,commodityId));
    }

    /**
     * 添加主评论/子评论
     * @param comment 评论
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public Dto<Comment> addComment(Comment comment) throws Exception{
        if(comment.getFatherId()==0){//添加主评论
            Comment commentBack=commentFeign.addMainComment(comment);
            if(commentBack!=null){
                return DtoUtil.returnDataSuccess(commentBack);
            }else {
                return DtoUtil.returnFail("获取数据失败","6002");
            }
        }else{//添加子评论
            Integer result=commentFeign.addChildComment(comment);
            if(result>0){
                return DtoUtil.returnSuccess();
            }else {
                return DtoUtil.returnFail("添加失败", "6002");
            }
        }
    }

    /**
     * 修改主评论
     * @param comment
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public Dto<Comment> updateComment(Comment comment) throws Exception{
        Comment commentBack=commentFeign.updateComment(comment);
        if(commentBack!=null){
            return DtoUtil.returnDataSuccess(commentBack);
        }else {
            return DtoUtil.returnFail("获取数据失败","6002");
        }
    }

    /**
     * 点赞
     * @param like
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/like",method = RequestMethod.POST)
    public Dto isLikedOn(Like like) throws Exception{
        int result=commentFeign.isLikedOn(like);
        if(result>0){//查到记录则取消点赞
            int jie1=commentFeign.delLike(like);
            if(jie1>0){
                return DtoUtil.returnSuccess();
            }else{
                return DtoUtil.returnFail("操作失败", "6002");
            }
        } else{//未查到记录则点赞
            int jie1=commentFeign.addLike(like);
            if(jie1>0){
                return DtoUtil.returnSuccess();
            }else{
                return DtoUtil.returnFail("操作失败", "6002");
            }
        }
    }


}
